home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-03-25 | 9.4 KB | 332 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CNeuroSimApp.cp ©1996 Timo Eloranta
- // ===========================================================================
- // An application class derived from the PowerPlant LApplication class.
- // This class handles the menu commands and owns the neural net and the
- // window inside of which our pane object is.
-
- #include "CNeuroSimApp.h"
-
- #include <LApplication.h> // PowerPlant classes
- #include <LCaption.h> //
- #include <LGrowZone.h> //
- #include <LPicture.h> //
- #include <LTabGroup.h> //
- #include <LWindow.h> //
-
- #include <LGAPushButton.h> // PP grayscale classes
- #include <LGAPrimaryBox.h> //
- #include <LGASecondaryBox.h> //
- #include <LGAEditField.h> //
- #include <LGADialogBox.h> //
-
- #include <UDesktop.h> // Deactivate & Activate
- #include <UDrawingState.h> // UQDGlobals
- #include <UModalDialogs.h> // StDialogHandler
- #include <UMemoryMgr.h> // InitializeHeap
- #include <URegistrar.h> // RegisterClass
-
- #include <PP_Messages.h> // cmd_New, msg_OK
-
- #include "CStdNeuralNet.h" // The only net type we have implemented
-
- #include "CNeuroSimPane.h" // Our pane class
- #include "CParamsDialog.h" // Our dialog class
-
- #include "CMyCaption.h" // My own caption class
- #include "CAGASlider.h" // James Jennings' grayscale slider class
-
-
- // ===========================================================================
- // • Main Program
- // ===========================================================================
- // A typical main function of a PowerPlant based Mac application
-
- void main()
- {
- // Set Debugging options
- #ifdef Debug_Throw
- SetDebugThrow_(debugAction_Alert);
- #endif
- #ifdef Debug_Signal
- SetDebugSignal_(debugAction_Alert);
- #endif
-
- InitializeHeap(3); // Initialize Memory Manager
-
- // Initialize standard Toolbox managers
- UQDGlobals::InitializeToolbox( &qd );
-
- // Initialize randomizer seed
- ::GetDateTime((unsigned long *)&qd.randSeed);
-
- new LGrowZone(20000); // Install a GrowZone function to catch
- // low memory situations.
-
- CNeuroSimApp theApp; // Create instance of our application
- theApp.Run(); // class and run it
- }
-
-
- // ===========================================================================
- // • CNeuroSimApp Class
- // ===========================================================================
-
- // ---------------------------------------------------------------------------
- // • CNeuroSimApp
- //
- // Called by: main()
- // ---------------------------------------------------------------------------
- // Constructor
-
- CNeuroSimApp::CNeuroSimApp()
- {
- RegisterClasses();
-
- // Initialize parameters with default values
- // (the defaults are defined in NS_Types.h).
-
- SGenParams theTemp
- = { DEFAULT_LENGTH_X_AVG, DEFAULT_LENGTH_Y_AVG,
- DEFAULT_LENGTH_X_DEV, DEFAULT_LENGTH_Y_DEV,
- DEFAULT_NET_SIZE,
- DEFAULT_QTY_MIN, DEFAULT_QTY_MAX };
-
- mParams = theTemp;
-
- mNet = NULL;
- mWindow = NULL;
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CNeuroSimApp
- // ---------------------------------------------------------------------------
- // Destructor
-
- CNeuroSimApp::~CNeuroSimApp()
- {
- if ( mWindow ) delete mWindow;
- if ( mNet ) delete mNet;
- }
-
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand
- //
- // Called by: LCommander::ProcessCommand
- // ---------------------------------------------------------------------------
- // Respond to commands
-
- Boolean
- CNeuroSimApp::ObeyCommand(
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
- CParamsDialog * theDialog;
-
- switch (inCommand) {
-
- // cmd_New, cmd_Params and cmd_Demo are menu commands
-
- case cmd_New:
- InitNewNet();
- break;
-
- case cmd_Params:
- theDialog = (CParamsDialog *)
- LWindow::CreateWindow( WIND_Params, this);
-
- theDialog -> InitDialog();
- theDialog -> SetValues( mParams );
- theDialog -> Show();
- break;
-
- case cmd_Demo:
- if ( mNet )
- mNet -> ChangeDemoMode();
- break;
-
- // cmd_SetParams is the command which is sent by
- // the parameters dialog if the user presses "OK".
- // We respond to it by reading in the new parameter
- // values before throwing the dialog away.
-
- case cmd_SetParams:
- theDialog = (CParamsDialog *)
- ((SLGADialogResponse *) ioParam) -> dialogBox;
-
- theDialog -> GetValues( mParams );
- delete theDialog;
- break;
-
- // By default we pass the command to our base class
- // which handles cmd_About and cmd_Quit
-
- default:
- cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return cmdHandled;
- }
-
- // ---------------------------------------------------------------------------
- // • FindCommandStatus
- //
- // Called by: LCommander::ProcessCommandStatus
- // ---------------------------------------------------------------------------
- // Pass back whether a Command is enabled and/or marked (in a Menu)
-
- void
- CNeuroSimApp::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName)
- {
- outUsesMark = false;
-
- switch (inCommand) {
-
- case cmd_New:
- case cmd_Params:
- outEnabled = true;
- break;
-
- // The text of the "demo mode" menu item is set dynamically
- // to "Start Demo" or "Stop Demo" depending on whether the
- // mode is currently ON or OFF.
-
- case cmd_Demo:
- if (mNet) {
- outEnabled = true;
- if (mNet -> DemoModeOn())
- ::GetIndString( outName, STRx_Menus, str_StopDemo );
- else
- ::GetIndString( outName, STRx_Menus, str_StartDemo );
- } else
- outEnabled = false;
- break;
-
- // We let our base class handle all the other commands
-
- default:
- LApplication::FindCommandStatus(inCommand, outEnabled,
- outUsesMark, outMark, outName);
- break;
- }
- }
-
- // ---------------------------------------------------------------------------
- // • InitNewNet
- //
- // Called by: CNeuroSimApp::ObeyCommand
- // ---------------------------------------------------------------------------
- // Create a new neural net and the window to display it in.
-
- void
- CNeuroSimApp::InitNewNet()
- {
- CNeuroSimPane * thePane;
-
- // Throw out the old stuff if any
-
- if ( mWindow ) delete mWindow;
- if ( mNet ) delete mNet;
-
- // Create and initialize a new net object.
-
- mNet = new CStdNeuralNet( mParams );
-
- // Create the main window from a resource
- // (our pane is inside the window)
-
- mWindow = LWindow::CreateWindow( WIND_NeuroSim, this );
- thePane = (CNeuroSimPane *) mWindow -> FindPaneByID( pane_NeuralNet );
-
- // "Introduce" the net and the pane to each other.
-
- thePane -> SetNet( mNet );
- mNet -> SetPane( thePane );
-
- mWindow -> Show();
- }
-
- // ---------------------------------------------------------------------------
- // • ShowAboutBox
- //
- // Called by: LApplication::ObeyCommand
- // ---------------------------------------------------------------------------
- // Display the About Box for NeuroSim
-
- void
- CNeuroSimApp::ShowAboutBox()
- {
- StDialogHandler theHandler( WIND_NeuroSimAbout, this );
- LWindow *theDialog = theHandler.GetDialog();
-
- if ( theDialog ) {
- while ( true ) {
- MessageT hitMessage = theHandler.DoDialog();
-
- if ( hitMessage == msg_OK )
- break;
- }
- } else { // This is only for the case where
- UDesktop::Deactivate(); // we don't have enough memory
- ::Alert( ALRT_About, nil ); // to show the finer About Box
- UDesktop::Activate(); // requested above...
- }
- }
-
- // ---------------------------------------------------------------------------
- // • RegisterClasses
- //
- // Called by: CNeuroSimApp::CNeuroSimApp
- // ---------------------------------------------------------------------------
- // Register classes for objects created from 'PPob' resources
-
- void
- CNeuroSimApp::RegisterClasses()
- {
- // --- Traditional PowerPlant classes --- //
-
- URegistrar::RegisterClass( LCaption ::class_ID, (ClassCreatorFunc)
- LCaption ::CreateCaptionStream);
- URegistrar::RegisterClass( LPane ::class_ID, (ClassCreatorFunc)
- LPane ::CreatePaneStream);
- URegistrar::RegisterClass( LPicture ::class_ID, (ClassCreatorFunc)
- LPicture ::CreatePictureStream);
- URegistrar::RegisterClass( LTabGroup ::class_ID, (ClassCreatorFunc)
- LTabGroup ::CreateTabGroupStream);
- URegistrar::RegisterClass( LView ::class_ID, (ClassCreatorFunc)
- LView ::CreateViewStream);
- URegistrar::RegisterClass( LWindow ::class_ID, (ClassCreatorFunc)
- LWindow ::CreateWindowStream);
-
- // --- PowerPlant's grayscale appearance classes --- //
-
- URegistrar::RegisterClass ( LGAPushButton ::class_ID, (ClassCreatorFunc)
- LGAPushButton ::CreateFromStream );
- URegistrar::RegisterClass ( LGAPrimaryBox ::class_ID, (ClassCreatorFunc)
- LGAPrimaryBox ::CreateFromStream );
- URegistrar::RegisterClass ( LGASecondaryBox ::class_ID, (ClassCreatorFunc)
- LGASecondaryBox ::CreateFromStream );
- URegistrar::RegisterClass ( LGAEditField ::class_ID, (ClassCreatorFunc)
- LGAEditField ::CreateFromStream );
- URegistrar::RegisterClass ( LGADialogBox ::class_ID, (ClassCreatorFunc)
- LGADialogBox ::CreateFromStream );
-
- // --- Finally the non-PowerPlant classes --- //
-
- URegistrar::RegisterClass( CNeuroSimPane ::class_ID, (ClassCreatorFunc)
- CNeuroSimPane ::CreateNeuroSimPaneStream);
- URegistrar::RegisterClass( CParamsDialog ::class_ID, (ClassCreatorFunc)
- CParamsDialog ::CreateParamsDialogStream);
- URegistrar::RegisterClass( CMyCaption ::class_ID, (ClassCreatorFunc)
- CMyCaption ::CreateMyCaptionStream);
- CAGASlider::Register();
- }